package com.itheima.d8_thread_pool;

import java.util.concurrent.*;

/*
     目标：线程池处理任务
     需求 : 使用线程池处理Callable类型任务
 */
public class ThreadPoolTest3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 10, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(20), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        Future<Integer> future1 = executor.submit(new Cal(10));
        Future<Integer> future2 = executor.submit(new Cal(20));
        Future<Integer> future3 = executor.submit(new Cal(30));

        System.out.println(future1.get());
        System.out.println(future2.get());
        System.out.println(future3.get());

    }
}

class Cal implements Callable<Integer> {
    private int num;

    public Cal(int num) {
        this.num = num;
    }
    int sum = 0;

    @Override
    public Integer call() throws Exception {
        for (int i = 0; i <=num; i++) {
            sum+=i;
        }
        return sum;
    }



}
